// pages/mall/deal/deal.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    num: 1,
    productList: [{
        productId: 1,
        productName: "细胞存储——为健康护航 为生命备份",
        productPrice: "29800.00",
        buyersNum: 100
      },
      {
        productId: 2,
        productName: "青春不止心态，更赢在姿态",
        productPrice: "15000.00",
        buyersNum: 30
      },
      {
        productId: 3,
        productName: "新春特惠——牛转乾坤",
        productPrice: "25000.00",
        buyersNum: 30
      },
      {
        productId: 4,
        productName: "以美丽，定义不平凡",
        productPrice: "18888.00",
        buyersNum: 30
      },
      {
        productId: 5,
        productName: "纳米微光",
        productPrice: "24800.00",
        buyersNum: 30
      },
      {
        productId: 6,
        productName: "新品上市，等你解锁",
        productPrice: "10000.00",
        buyersNum: 30
      },
    ],
    useBalance:'',
    usePoints:'',
  },
  reduceNum() {
    let c_num = this.data.num;
    if (c_num == 1) {
      wx.showToast({
        title: '数量不能小于1',
        icon: "error"
      })
    } else {
      c_num = c_num - 1;
      this.setData({
        num: c_num
      })
    }
    this.jisuan()
  },
  inputNum(e) {
    let c_num = e.detail.value;
    // console.log(num)
    this.setData({
      num: c_num
    })
    this.jisuan()
  },
  addNum() {
    let c_num = this.data.num;

    c_num = c_num + 1;
    this.setData({
      num: c_num
    })
    this.jisuan()
  },
  inputBalance(e){
    let c_balance = e.detail.value;
    if(c_balance > wx.getStorageSync('balance')){
      wx.showToast({
        title: '可用余额不足',
        icon:"error"
      })
      this.setData({
        useBalance:wx.getStorageSync('balance')
      })
    }else{
      this.setData({
        useBalance:c_balance
      })
    }
    this.jisuan()
  },
  inputPoints(e){
    let c_points = e.detail.value;
    if(c_points > wx.getStorageSync('points')){
      wx.showToast({
        title: '可用积分不足',
        icon:"error"
      })
      this.setData({
        usePoints:wx.getStorageSync('points')
      })
    }else{
      this.setData({
        usePoints:c_points
      })
    }
    this.jisuan()
  },
  jisuan(){
    let num = this.data.num;
    let price = this.data.productInfo.productPrice;
    let total = num * price;
    let useBalance = this.data.useBalance;
    let usePoints = this.data.usePoints;
    let heji = total - useBalance - usePoints;
    this.setData({
      heji
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options)
    let productId = options.productId
    this.setData({
      productInfo: this.data.productList[productId - 1],
      balance:wx.getStorageSync('balance'),
      points:wx.getStorageSync('points'),
    })
    this.setData({
      heji:this.data.productInfo.productPrice
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})